package com.haitao.glide.progress;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.AppGlideModule;

import java.io.File;
import java.io.InputStream;


@GlideModule
public class ProgressAppGlideModule extends AppGlideModule {
    public static String cachePath;

    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
        super.registerComponents(context, glide, registry);
        registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(ProgressManager.getOkHttpClient()));
    }

    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        super.applyOptions(context, builder);
        //ViewTarget.setTagId(R.id.glide_tag_id);
        // builder.set(DecodeFormat.PREFER_ARGB_8888);
        if (context.getExternalCacheDir() == null) {
            cachePath = new File(context.getCacheDir().getPath() + File.separator).getAbsolutePath();
        } else {
            cachePath = new File(context.getExternalCacheDir().getPath() + File.separator).getAbsolutePath();
        }
        Log.e("cachePath", cachePath);
        builder.setDiskCache(new DiskLruCacheFactory(cachePath, 150 * 1024 * 1024));
    }
}